package com.cssl.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cssl.pojo.Result;
import com.cssl.pojo.TbItemGoods;
import com.cssl.service.TbItemGoodsService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/goods")
//@CrossOrigin
public class TbItemGoodsController {
    @Resource
    private TbItemGoodsService service;

    /**
     * 商品添加
     * @param tbItemGoods
     * @return
     */
    @PostMapping("/goodsInsert")
    public TbItemGoods insert(@RequestBody TbItemGoods tbItemGoods){
        System.out.println("进入添加");
        if(service.saveOrUpdate(tbItemGoods))
            return tbItemGoods;
        return null;
    }

    /**
     * 商品修改
     * @param tbItemGoods
     * @return
     */
    @RequestMapping("/goodsUpdate")
    public Result<TbItemGoods> updateMember(TbItemGoods tbItemGoods) {
        System.out.println("修改会员:" + tbItemGoods);
        //根据id改
        service.updateById(tbItemGoods);
        return Result.ok(service.getById(tbItemGoods.getGoods_id()));
    }
  /*  *//**
     * 商品删除
     * @param car_id
     * @return
     *//*
    @GetMapping("/goodsDelete")
    public boolean delete(@RequestParam("car_id") Integer car_id){
        System.out.println("dsadsadasdsa************");
        return service.removeById(car_id);
    }*/

    /**
     * 商品分页查询
     * @param tbItemGoods
     * @param index
     * @param size
     * @return
     */
    @GetMapping("/goodsPage")
    public Result page(TbItemGoods tbItemGoods,
                       @RequestParam(name = "index") int index,
                       @RequestParam(name = "size") int size){
        return Result.ok(service.page(new Page<TbItemGoods>(index,size)));
    }

    /**
     * 根据商品名称模糊查询
     * @param tbItemGoods
     * @return
     */
    @GetMapping("/goodsQuery")
    public Result query(TbItemGoods tbItemGoods,
                        @RequestParam(name="index" ) int index,
                        @RequestParam(name="size") int size){
        System.out.println("dasdasdsa------------------");
        LambdaQueryWrapper<TbItemGoods> lqw = Wrappers.lambdaQuery();
        lqw.like(tbItemGoods.getGoods_name()!=null,
                TbItemGoods::getGoods_name,tbItemGoods.getGoods_name());
        lqw.orderByDesc(TbItemGoods::getGoods_id);
        return Result.ok(service.page(
                new Page<TbItemGoods>(index,size),lqw));
    }

    @GetMapping("/goodsStatus")
    public Result status(TbItemGoods tbItemGoods){
        return Result.ok(service.update(
                            new UpdateWrapper<TbItemGoods>()
                                .eq("goods_id",tbItemGoods.getGoods_id())//根据这个去修改
                                .set("status",tbItemGoods.getStatus())));//只修改这个列
    }

    @DeleteMapping("/goodsDelete/api/{goods_id}")
    public Result delete(@PathVariable("goods_id") Integer goods_id){
        boolean success = service.removeById(goods_id);
        if (success) {
            return Result.ok();
        } else {
            return Result.error("删除失败");
        }
    }
}
